#include <stdio.h>
#include <stdlib.h>

#define     MAX_INT 10001

//gloabl val
int nrow = 0, ncol = 0;
int** lenmatrix = NULL;
int maxlen = 0;

int max1(int a, int b, int c, int d)
{
    int maxab, maxcd = 0;

    maxab = a > b ? a : b;
    maxcd = c > d ? c : d;

    return maxab > maxcd ? maxab : maxcd;
}

void showmatrix(int** matrix)
{
    printf("%d\r\n", maxlen);
}

//xrow, xcol
int getVal(int** matrix, int xrow, int xcol)
{
    if(xrow < 0 || xrow >= nrow)
        return MAX_INT;
    if(xcol < 0 || xcol >= ncol)
        return MAX_INT;

    return matrix[xrow][xcol];

}

int getlen(int** matrix, int xrow, int xcol)
{
    int len = 0;
    int valmid = getVal(matrix, xrow, xcol);
    int lenup = 0, valup = 0;
    int lendn = 0, valdn = 0;
    int lenrt = 0, valrt = 0;
    int lenlt = 0, vallt = 0;

    len = lenmatrix[xrow][xcol];
    if(len != -1)
        return len;

    if(xrow < 0 || xrow >= nrow || xcol < 0|| xcol >= ncol)
        return 10001;

    valup = getVal(matrix, xrow-1, xcol);
    if(valup >= valmid)
        lenup = 1;
    else
        lenup = getlen(matrix, xrow - 1, xcol) + 1;

    valdn = getVal(matrix, xrow+1, xcol);
    if(valdn >= valmid)
        lendn = 1;
    else
        lendn = getlen(matrix, xrow + 1, xcol) + 1;

    valrt = getVal(matrix, xrow, xcol+1);
    if(valrt >= valmid)
        lenrt = 1;
    else
        lenrt = getlen(matrix, xrow, xcol+1) + 1;

    vallt = getVal(matrix, xrow, xcol-1);
    if(vallt >= valmid)
        lenlt = 1;
    else
        lenlt = getlen(matrix, xrow , xcol-1) + 1;

    lenmatrix[xrow][xcol] = max1(lenup, lendn, lenrt, lenlt);
    if(maxlen < lenmatrix[xrow][xcol])
        maxlen = lenmatrix[xrow][xcol];
    return max1(lenup, lendn, lenrt, lenlt);
}

void fn(int** matrix)
{
    int i = 0, j = 0;

    for(i = 0; i < nrow; i++)
    {
        for(j = 0; j < ncol; j++)
        {
            getlen(matrix, i, j);
        }
    }
}

void freespace(int **matrix)
{
    int i = 0;
    for(i = 0; i < nrow; i++)
    {
        free(matrix[i]);
        free(lenmatrix[i]);
    }
    free(matrix);
    free(lenmatrix);
}

int main()
{
    int **matrix = NULL;
    int i = 0, j = 0;

    //freopen("bin\\debug\\in.txt", "r", stdin);

    scanf("%d %d", &nrow, &ncol);

    lenmatrix = (int**)malloc(nrow * sizeof(int*));
    for(i = 0; i < nrow; i++)
    {
        lenmatrix[i] = (int*)malloc(ncol * sizeof(int));
        for(j = 0; j < ncol; j++)
            lenmatrix[i][j] = -1;
    }

    matrix = (int**)malloc(nrow * sizeof(int*));
    for(i = 0; i < nrow; i++)
    {
        matrix[i] = (int*)malloc(ncol * sizeof(int));
    }

    for(i = 0; i < nrow; i++)
    {
        for(j = 0; j < ncol; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }


    fn(matrix);

    showmatrix(matrix);

    freespace(matrix);
    return 0;
}
